import redis as red
from functools import partial




class RedisCoon:
    con = red.Redis(host='123.206.210.251', port=6379, db=0, password='pass')

    def __init__(self, namespace=None):
        if namespace is not None:
            self.exists = partial(self.exists, key=namespace)
            self.get = partial(self.get, key=namespace)
            self.rpush = partial(self.rpush, key=namespace)
            self.lpush = partial(self.lpush, key=namespace)
            self.pop = partial(self.pop, key=namespace)

    def exists(self, key):
        return RedisCoon.con.exists(key)

    def get(self, key):
        data = None
        try:
            data = str(RedisCoon.con.get(key), encoding="utf-8")
        except Exception as e:
            print(e)

        return data

    def rpush(self, key, value):
        RedisCoon.con.rpush(key, *value)

    def lpush(self, key, value):
        RedisCoon.con.lpush(key, *value)

    def pop(self, key):
        if not self.exists(key):
            return None
        try:
            data = RedisCoon.con.brpop(key)
            data = str(data[1], encoding="utf-8")
        except Exception as e:
            print(e)
            data = None

        return data






ctx = RedisCoon()





